package com.eshipenet.shipnet.edgeboxrpc.util;

import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Timestamps;

import java.util.Date;

public class TimestampToDate {

    static final long MILLIS_PER_SECOND = 1000;

    /**
     * google.proto.timestamp -> java.util.Date
     */
    public static Date timestampToDate(Timestamp timestamp) {
        return new Date(timestamp.getSeconds() * MILLIS_PER_SECOND);
    }

    /**
     * java.util.Date -> google.proto.timestamp
     *
     * @return
     */
    public static com.google.protobuf.Timestamp dateTotimestamp(Date date) {

        if (date == null) {
            return null;
        }
        return Timestamps.fromMillis(date.getTime());
    }


    public static void main(String[] args) {
        Date date = new Date();
        long unixTimestamp = date.getTime() / 1000;
        com.google.protobuf.Timestamp a = Timestamps.fromMillis(unixTimestamp);

        System.out.println("3:" + dateTotimestamp(null));


    }
}
